package com.ruoyi.teaching.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.TeachingStudentMapper;
import com.ruoyi.teaching.domain.TeachingStudent;
import com.ruoyi.teaching.service.ITeachingStudentService;

/**
 * 学生信息Service业务层处理
 * 
 * @author suben
 * @date 2021-02-07
 */
@Service
public class TeachingStudentServiceImpl implements ITeachingStudentService 
{
    private static final Logger log = LoggerFactory.getLogger(TeachingStudentServiceImpl.class);

    @Autowired
    private TeachingStudentMapper teachingStudentMapper;

    /**
     * 查询学生信息
     * 
     * @param studentId 学生信息ID
     * @return 学生信息
     */
    @Override
    public TeachingStudent selectTeachingStudentById(Long studentId)
    {
        return teachingStudentMapper.selectTeachingStudentById(studentId);
    }

    /**
     * 查询学生信息列表
     * 
     * @param teachingStudent 学生信息
     * @return 学生信息
     */
    @Override
    public List<TeachingStudent> selectTeachingStudentList(TeachingStudent teachingStudent)
    {
        return teachingStudentMapper.selectTeachingStudentList(teachingStudent);
    }

    /**
     * 新增学生信息
     * 
     * @param teachingStudent 学生信息
     * @return 结果
     */
    @Override
    public int insertTeachingStudent(TeachingStudent teachingStudent)
    {
        return teachingStudentMapper.insertTeachingStudent(teachingStudent);
    }

    /**
     * 修改学生信息
     * 
     * @param teachingStudent 学生信息
     * @return 结果
     */
    @Override
    public int updateTeachingStudent(TeachingStudent teachingStudent)
    {
        return teachingStudentMapper.updateTeachingStudent(teachingStudent);
    }

    /**
     * 批量删除学生信息
     * 
     * @param studentIds 需要删除的学生信息ID
     * @return 结果
     */
    @Override
    public int deleteTeachingStudentByIds(Long[] studentIds)
    {
        return teachingStudentMapper.deleteTeachingStudentByIds(studentIds);
    }

    /**
     * 删除学生信息信息
     * 
     * @param studentId 学生信息ID
     * @return 结果
     */
    @Override
    public int deleteTeachingStudentById(Long studentId)
    {
        return teachingStudentMapper.deleteTeachingStudentById(studentId);
    }

    /**
     * 导入学生数据
     *
     * @param userList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importStudent(List<TeachingStudent> userList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TeachingStudent user : userList)
        {
            try
            {
                // 验证是否存在这个用户
                TeachingStudent u = teachingStudentMapper.selectTeachingStudentByName(user.getStudentName());
                if (StringUtils.isNull(u))
                {
                    user.setCreateBy(operName);
                    this.insertTeachingStudent(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getStudentName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    user.setUpdateBy(operName);
                    this.updateTeachingStudent(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getStudentName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getStudentName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getStudentName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
